﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapGIS.GeoObjects.Geometry;
using MapGIS.GeoObjects.Info;
using MapGIS.GeoMap;

namespace Wcf_MapFrame
{
    static class TranGeoJson
    {
        private static string TransDot(double x, double y)
        {
            return string.Format("[{0},{1}]", x, y);
        }
        private static string GetAnnFont(TextAnnInfo info)
        {
            string strRt = "";
            float size = info.Width;
            int ifnt = info.Ifnt;
            int nPx = (int)(size * 96 / 25.4);
            SystemLibrarys sysLibs = SystemLibrarys.GetSystemLibrarys();
            if (sysLibs != null)
            {
                //获取缺省系统库
                Guid guid = new Guid();
                SystemLibrary sysLib = sysLibs.GetSystemLibrary(guid);
                if (sysLib != null)
                {
                    //获取字体库
                    FontLibrary fontLib = sysLib.GetFontLibrary();
                    if (fontLib != null)
                    {
                        //根据字体ID获取字体对象（ID从0开始）
                        MapGIS.GeoMap.Font font = fontLib.GetFont(info.Ifnt);
                        strRt = "normal " + nPx.ToString() + "px " + font.Name;
                    }
                }
            }
            return strRt;
        }

        public static string TransPoint(GeoPoint point)
        {
            string str = "{ \"type\": \"Feature\",\"geometry\":{ \"type\": \"Point\",\"coordinates\": ";
            string geostr = TransDot(point.Get().X, point.Get().Y);
            str = str + geostr + "}}";
            return str;
        }

        public static string TransPoints(GeoPoints points)
        {
            if (points.Count <= 0)
                return "";
            string str = "{ \"type\": \"Feature\",\"geometry\":{ \"type\": \"MultiPoint\",\"coordinates\": [";
            string geostr = TransDot(points.GetItem(0).X, points.GetItem(0).Y);
            for (int i = 1; i < points.Count; i++)
            {
                geostr += ",";
                geostr += TransDot(points.GetItem(i).X, points.GetItem(i).Y);
            }
            str = str + geostr + "]}}";
            return str;
        }

        public static string TransLin(GeoVarLine lin)
        {
            if (lin.DotNum <= 0)
                return "";
            string str = "{ \"type\": \"Feature\",\"geometry\":{ \"type\": \"LineString\",\"coordinates\": [";
            string geostr = TransDot(lin.get_X(0), lin.get_Y(0));
            for (int i = 1; i < lin.DotNum; i++)
            {
                geostr += ",";
                geostr += TransDot(lin.get_X(i), lin.get_Y(i));
            }
            str = str + geostr + "]}}";
            return str;
        }

        public static string TransLins(GeoLines lins)
        {
            if (lins.Count <= 0)
                return "";
            string str = "{ \"type\": \"Feature\",\"geometry\":{ \"type\": \"MultiLineString\",\"coordinates\": [";
            for (int i = 0; i < lins.Count; i++)
            {
                str += "[";
                IGeoLine lin = lins.get_Item(i);
                if (lin != null && lin is GeoVarLine)
                {
                    GeoVarLine varlin = lin as GeoVarLine;
                    for (int j = 0; j < varlin.DotNum; j++)
                    {
                        str += TransDot(varlin.get_X(j), varlin.get_Y(j));
                        if (j != varlin.DotNum - 1)
                            str += ",";
                    }

                }
                str += "]";
                if (i != lins.Count - 1)
                    str += ",";
            }
            str = str + "]}}";
            return str;
        }

        public static string TransPolygon(GeoPolygon polygon)
        {
            if (polygon.CircleNum <= 0)
                return "";
            string str = "{ \"type\": \"Feature\",\"geometry\":{ \"type\": \"Polygon\",\"coordinates\": [";
            for (int i = 0; i < polygon.CircleNum; i++)
            {
                str += "[";
                GeoLines lins = polygon.Get(i);
                if (lins != null)
                {
                    GeoVarLine varlin = lins.get_Item(0) as GeoVarLine;
                    if (varlin != null)
                    {
                        for (int j = 0; j < varlin.DotNum; j++)
                        {
                            str += TransDot(varlin.get_X(j), varlin.get_Y(j));
                            if (j != varlin.DotNum - 1)
                                str += ",";
                        }
                    }
                }
                str += "]";
                if (i != polygon.CircleNum - 1)
                    str += ",";
            }
            str = str + "]}}";
            return str;
        }

        public static string TransPolygons(GeoPolygons polygons)
        {
            string str = "{ \"type\": \"Feature\",\"geometry\":{ \"type\": \"MultiPolygon\",\"coordinates\": [";

            for (int i = 0; i < polygons.Count; i++)
            {
                str += "[";
                GeoPolygon polygon = polygons[i];

                for (int j = 0; j < polygon.CircleNum; j++)
                {
                    str += "[";
                    GeoLines lins = polygon.Get(j);
                    if (lins != null)
                    {
                        GeoVarLine varlin = lins.get_Item(0) as GeoVarLine;
                        if (varlin != null)
                        {
                            for (int k = 0; k < varlin.DotNum; k++)
                            {
                                str += TransDot(varlin.get_X(k), varlin.get_Y(k));
                                if (k != varlin.DotNum - 1)
                                    str += ",";
                            }
                        }
                    }
                    str += "]";
                    if (j != polygon.CircleNum - 1)
                        str += ",";
                }

                str += "]";
                if (i != polygons.Count - 1)
                    str += ",";
            }
            str = str + "]}}";
            return str;
        }
        public static string TransText(TextAnno annoText)
        {
            string font = "", text = annoText.Text;
            font = GetAnnFont(annoText.GetAnnInfo() as TextAnnInfo);
            string str = "{ \"type\": \"Feature\",\"geometry\":{ \"type\": \"Point\",\"coordinates\": ";
            str += TransDot(annoText.AnchorDot.X, annoText.AnchorDot.Y);
            str += "},";
            str += "\"style\":{\"text\":{";
            font = "\"font\": \"" + font + "\",";
            str += font;
            text = "\"text\": \"" + text + "\",";
            str += text;
            str += "\"fill\": \"\"";
            str += "}}}";
            return str;
        }

        public static string TransSRS(string srs)
        {
            string str = "\"crs\": {\"type\": \"name\",\"properties\": {\"name\": \"";
            str += srs;
            str += "\"}}";
            return str;
        }
    }
}
